//
//  LYCollectionPageViewController.m
//  LYKitDemo
//
//  Created by Mac-Mini-KingVision on 16/2/22.
//  Copyright © 2016年 LiuWenYang. All rights reserved.
//

#import "LYCollectionPageViewController.h"
#import "Masonry.h"

@interface LYCollectionPageViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

/** 展示的collectionView */
@property (nonatomic,strong) UICollectionView *collectionView;
/** views */
@property (nonatomic,strong) NSArray *views;


@end

@implementation LYCollectionPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.trailing.leading.bottom.equalTo(self.view);
    }];
    
//    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    
    // Do any additional setup after loading the view.
}


-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
}

-(UICollectionView *)collectionView
{
    if (_collectionView == nil) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.pagingEnabled = YES;
        _collectionView.bounces = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CellPageID"];
    }
    return _collectionView;
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.views.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellPageID" forIndexPath:indexPath];
    if (cell == nil) {
        cell  = [[UICollectionViewCell alloc] init];
    }
    
    // 移除之前的子控件
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    // 添加控制器
    UIView *view = [self.views objectAtIndex:indexPath.row];
    
    view.frame = CGRectMake(0, 0, self.collectionView.frame.size.width ,self.collectionView.frame.size.height);
    
    [cell.contentView addSubview:view];
    
    return cell;

}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return collectionView.frame.size;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSArray *paths = self.collectionView.indexPathsForVisibleItems;
    NSIndexPath *currentPageIndex = [paths firstObject];
    _current = currentPageIndex.row;
    if (self.delegate && [self.delegate respondsToSelector:@selector(lyCollectionPageViewController:currentPage:)]) {
        [self.delegate lyCollectionPageViewController:self currentPage:self.current];
    }
}

-(void)setCurrent:(NSInteger)current
{
    _current = current;
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:current inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}

-(void)setUpWithViews:(NSArray *)views
{
    _views = views;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
